package com.linxz.ui.widgets;

import android.content.Context;
import android.util.AttributeSet;

import androidx.annotation.NonNull;

import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.constant.SpinnerStyle;
import com.scwang.smartrefresh.layout.footer.BallPulseFooter;
import com.scwang.smartrefresh.layout.header.ClassicsHeader;

/**
 * @author Linxz
 * 创建日期：2019年02月21日 14:15
 * version：1.0.0
 * 描述：
 */
public class LinxzRefreshLayout extends SmartRefreshLayout {
    public LinxzRefreshLayout(Context context) {
        super(context);
        initRefresh(context);
    }

    public LinxzRefreshLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
        initRefresh(context);
    }

    public LinxzRefreshLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initRefresh(context);
    }

    private void initRefresh(Context context){
        this.setRefreshHeader(new ClassicsHeader(context));
        this.setHeaderHeight(60);
        //设置 Footer 为 球脉冲 样式
        this.setRefreshFooter(new BallPulseFooter(context).setSpinnerStyle(SpinnerStyle.Scale));
        setOnRefreshListener(mOnRefreshListener);
    }

    public interface OnRefreshListener{
        public void onRefresh();
    }

    private OnRefreshListener onRefreshListener;

    public void setOnRefreshListener(OnRefreshListener onRefreshListener){
        this.onRefreshListener=onRefreshListener;
    }

    @Override
    public SmartRefreshLayout finishRefresh() {
        return super.finishRefresh();
    }

    @Override
    protected void setStateRefreshing() {
        super.setStateRefreshing();
    }

    private com.scwang.smartrefresh.layout.listener.OnRefreshListener mOnRefreshListener=new com.scwang.smartrefresh.layout.listener.OnRefreshListener(){
        @Override
        public void onRefresh(@NonNull RefreshLayout refreshLayout) {
            if (onRefreshListener!=null){
                onRefreshListener.onRefresh();
            }
        }
    };

}
